package session;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import persistence.ClienteFin;

import util.SqlConnection;
import bean.ClienteFinBean;

/**
 * @since 2011.06.13
 * @author mhurtado
 *
 */

public class ClienteFinSession{
	
	private SqlSession  session;
	
	public ClienteFinSession(){
				
	}

	public ArrayList<ClienteFinBean> listarClienteFin() throws Exception {
		ArrayList<ClienteFinBean> list= null;
		try {			
			list = getSession().listarCliente();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}
	
	public ArrayList<ClienteFinBean> listarClienteFin(ClienteFinBean clienteFinBean) throws Exception {
		ArrayList<ClienteFinBean> list= null;
		try {			
			list = getSession().listarClienteFin(clienteFinBean);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	public void guardarClienteFin(ClienteFinBean clienteFinBean)
			throws Exception {
		try {
			session = SqlConnection.getSessionFactory().openSession();			
			ClienteFin clienteFin = session.getMapper(ClienteFin.class);
			clienteFin.guardarClienteFin(clienteFinBean);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
	}

	public void editarClienteFin(ClienteFinBean clienteFinBean)
			throws Exception {
		try {
			//getSession().editarClienteFin(clienteFinBean);
			session = SqlConnection.getSessionFactory().openSession();			
			ClienteFin clienteFin =session.getMapper(ClienteFin.class);
			clienteFin.editarClienteFin(clienteFinBean);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
	}

	public void eliminarClienteFin(String id)
			throws Exception {
		try {
			getSession().eliminarClienteFin(id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
	}
	
	public ClienteFinBean buscarIdClienteFinBean(String id) throws Exception {
		ClienteFinBean clienteFinBean=null;
		try {
			clienteFinBean = new ClienteFinBean();
			session = SqlConnection.getSessionFactory().openSession();			
			clienteFinBean =session.getMapper(ClienteFin.class).buscarIdClienteFinBean(id);
			clienteFinBean = getSession().buscarIdClienteFinBean(id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return clienteFinBean;
	}


	public ClienteFin getSession(){
		ClienteFin clienteFin=null;
		try {
			session = SqlConnection.getSessionFactory().openSession();			
			clienteFin= session.getMapper(ClienteFin.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clienteFin;
	}
}
